/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import java.io.Serializable;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * Class corresponding to the "exports" element of the exports_conf.xml file.
 * This defines the exports configuration object.
 */
public class JBExports implements Serializable
{
    /** Set of the exports for all the profiles. */
    private Map globalExportsMap = new HashMap();

    /** Set of the exports specific to a profile. */
    private Map exportProfilesMap = new HashMap();

    /**
     * Adds a global export configuration.
     *
     * @param jbExport  The export configuration to add.
     */
    public void addExport(JBExport jbExport)
    {
        String key = jbExport.getName().trim().toLowerCase();

        globalExportsMap.put(key, jbExport);
    }

    /**
     * Adds an export configuration specific to a profile.
     *
     * @param prof  The specific export configuration.
     */
    public void addExportProfile(JBExportProfile prof)
    {
        String key = prof.getName().trim().toLowerCase();

        exportProfilesMap.put(key, prof);
    }

    /**
     * Converts the object into a String.
     *
     * @return A String.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        if (globalExportsMap != null)
        {
            buf.append("Map components of globalExportsMap = ");
            buf.append(System.getProperty("line.separator"));
            it = globalExportsMap.keySet().iterator();

            Object key;

            while (it.hasNext())
            {
                buf.append("...");
                key = it.next();
                buf.append(key);
                buf.append(" = ");
                buf.append(globalExportsMap.get(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (exportProfilesMap != null)
        {
            buf.append("Map components of exportProfilesMap = ");
            buf.append(System.getProperty("line.separator"));
            it = exportProfilesMap.keySet().iterator();

            Object key;

            while (it.hasNext())
            {
                buf.append("...");
                key = it.next();
                buf.append(key);
                buf.append(" = ");
                buf.append(exportProfilesMap.get(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method

    /**
     * Gets the set of global exports.
     *
     * @return The global exports map.
     */
    public Map getGlobalExportsMap()
    {
        return globalExportsMap;
    }

    /**
     * Gets the set of the exports specific to a profile.
     *
     * @return The profile exports map.
     */
    public Map getExportProfilesMap()
    {
        return exportProfilesMap;
    }

    /**
     * Adds the global exports in the profile exports.
     */
    public void enrichProfileExports()
    {
        Iterator itExport = globalExportsMap.values().iterator();
        JBExport currentExport = null;

        while (itExport.hasNext())
        {
            currentExport = (JBExport) itExport.next();

            Iterator itProf = exportProfilesMap.values().iterator();
            JBExportProfile prof = null;

            while (itProf.hasNext())
            {
                prof = (JBExportProfile) itProf.next();

                // check if the export is redefined in this profile
                if (prof.findExportByName(currentExport.getName()) == null)
                {
                    prof.addExport(currentExport);
                }
            }
        }
    }

    /**
     * Finds an export configuration in the export maps in function
     *  of a profile name and a export name.
     * First in profile exports map, and after in the global exports map
     *  if the export configuration is not found.
     *
     * @param profileName  Name of the profile.
     * @param exportName   Name of the export configuration.
     *
     * @return The export configuration object.
     */
    public JBExport findExportByExportName(String profileName, String exportName)
    {
        // search in the profile, if no profile found, search in the global
           JBExportProfile prof = null;
           if (profileName != null)
           {
               prof = (JBExportProfile) exportProfilesMap.get(profileName.trim()
                       .toLowerCase());
           }

        String key = exportName.trim().toLowerCase();

        if (prof == null)
        {
            // no profile with this name available, send global profile
            return (JBExport) globalExportsMap.get(key);
        }
        else
        {
            // profile found
            return prof.findExportByName(key);
        }
    }

}
